﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMWeb.Pages.Test.GetResponseE_marketing
{
    public partial class SendNewsLetter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            sendNewsLetter();
        }

        public void sendNewsLetter()
        {
            String api_key = "ENTER_YOUR_API_KEY_HERE";
            String api_url = "http://api2.getresponse.com";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Hashtable _request = new Hashtable();
            try
            {
                _request["jsonrpc"] = "2.0";
                _request["id"] = 2;
                _request["method"] = "send_newsletter";

                Hashtable contact_params = new Hashtable();
                contact_params["campaign"] = txtCampaignsId.Text;
                contact_params["from_field"] = "Test";
                contact_params["reply_to_field"] = "Test";
                contact_params["subject"] = txtSubject.Text;
                contact_params["name"] = "test";

                Hashtable contents = new Hashtable();
                contents["plain"] = txtPlain.Text;
                contents["html"] = txtHtml.Text;
                object[] contents_array = { contents };
                contact_params["contents"] = contents_array;

                Hashtable attachments = new Hashtable();
                attachments["data"] = "WmHFvMOzxYLEhyBnxJnFm2zEhSBqYcW6xYQu==";
                attachments["name"] = "order.txt";
                attachments["mime"] = "application/txt";
                object[] attachments_array = { attachments };
                contact_params["attachments"] = attachments_array;

                contact_params["flags"] = "clicktrack";
                contact_params["contacts"] = "CONTACT_ID";
                contact_params["get_contacts"] = "get_contacts conditions";
                contact_params["segments"] = "SEGMENT_ID";
                contact_params["get_segments"] = "get_segments conditions";
                contact_params["suppressions"] = "SUPPRESSION_ID";
                contact_params["get_suppressions"] = "get_suppressions conditions";
             
                object[] add_contact_params_array = { api_key, contact_params };
                _request["params"] = add_contact_params_array;
                System.Net.ServicePointManager.Expect100Continue = false;
                String response_string = getRespose(_request, api_url);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string getRespose(Hashtable _request, string api_url)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            String response_string = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_url);
                request.Method = "POST";
                byte[] request_bytes = Encoding.UTF8.GetBytes(jss.Serialize(_request));
                Stream request_stream = request.GetRequestStream();
                request_stream.Write(request_bytes, 0, request_bytes.Length);
                request_stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream response_stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(response_stream);
                response_string = reader.ReadToEnd();
                reader.Close();
                response_stream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }
            return response_string;
        }
    }
}